package com.apes.fn.scm.merchant.settlement.model;

import com.apes.fn.fico.debitnote.model.BusinessDebitNote;
import com.apes.fn.fico.expense.ExpenseItem;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.merchant.settlement.model
 * @ClassName: MerchantBusinessDebitItem
 * @Author: pyb
 * @Description: 赋能商户结算借支明细
 * @CreateTime: 2020-10-29 16:21
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = MerchantBusinessDebitItem.CONST_TABLE_NAME)
public class MerchantBusinessDebitItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_merchant_business_item";
    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "赋能对账借支明细", padding = 8)
    @Comment("编码")
    private String id;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_id", referencedColumnName = "id")
    @Comment("借支单")
    private BusinessDebitNote businessDebitNote;

    @Column(name = "current_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("本期扣收金额")
    private double currentAmount = 0;

    @ManyToOne(optional = false)
    @JoinColumn(name = "merchant_settlement_id", referencedColumnName = "id", nullable = false)
    private MerchantSettlement merchantSettlement;


    @Transient
    private Date approveDate;

    @Transient
    public Date getApproveDate() {
        if (businessDebitNote == null) return null;
        return businessDebitNote.getApproveDate();

    }


    @Transient
    private Date loanDate;

    @Transient
    public Date getLoanDate() {
        if (businessDebitNote == null) return null;
        return businessDebitNote.getLoanDate();

    }


    @Transient
    private double priceDebit;

    @Transient
    public double getPriceDebit() {
        if (businessDebitNote == null) return 0;
        return businessDebitNote.getPriceDebit();

    }
    @Transient
    private double principalRepaid;

    @Transient
    public double getPrincipalRepaid() {
        if (businessDebitNote == null) return 0;
        return businessDebitNote.getPrincipalRepaid();

    }

    @Transient
    private double remainingDeductibleBalance;

    @Transient
    public double getRemainingDeductibleBalance() {
        if (businessDebitNote == null) return 0;
        return  businessDebitNote.getPriceDebit()-businessDebitNote.getPrincipalRepaid() -currentAmount;

    }
}
